//
//  UIImageView+GIF.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/30.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "UIImageView+GIF.h"

@implementation UIImageView (GIF)

- (void)gif:(NSString *)name {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    
    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    CGFloat animationTime = 0.f;
    if (src) {
        size_t l = CGImageSourceGetCount(src);
        frames = [NSMutableArray arrayWithCapacity:l];
        for (size_t i = 0; i < l; i++) {
            CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
            NSDictionary *properties = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
            NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
            NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
            
            animationTime += [delayTime floatValue];
            if (img) {
                UIImage *image = [UIImage imageWithCGImage:img];
                [frames addObject:image];
                CGImageRelease(img);
            }
        }
        CFRelease(src);
    }
    
    self.animationImages = frames;
    [self startAnimating];
}

- (void)gif:(NSString *)name repeat:(BOOL)repeat {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    
    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    CGFloat animationTime = 0.f;
    if (src) {
        size_t l = CGImageSourceGetCount(src);
        frames = [NSMutableArray arrayWithCapacity:l];
        for (size_t i = 0; i < l; i++) {
            CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
            NSDictionary *properties = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
            NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
            NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
            
            animationTime += [delayTime floatValue];
            if (img) {
                UIImage *image = [UIImage imageWithCGImage:img];
                [frames addObject:image];
                CGImageRelease(img);
            }
        }
        CFRelease(src);
    }
    
    self.animationImages = frames;
    
    [self startAnimating];
    
    if (repeat == false) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self stopAnimating];
            self.animationImages = nil;
            self.image = frames.lastObject;
        });
    }
}

@end
